package gang.com.downloader;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * Created by xiaogangzai on 2017/11/4.
 */

public class DownloadService extends Service {

    private HashMap<String, DownloadTask> mDownloadingTasks = new HashMap<>();
    private ExecutorService mExecutors;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mExecutors = Executors.newCachedThreadPool();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        DownloadEntry entry = (DownloadEntry) intent.getSerializableExtra(Constants.KEY_DOWNLOAD_ENTRY);
        int action = intent.getIntExtra(Constants.KEY_DOWNLOAD_ACTION, -1);
        //下载相关操作
        doAction(action, entry);

        return super.onStartCommand(intent, flags, startId);
    }

    private void doAction(int action, DownloadEntry entry) {
        //check action，do related action，下载参数，想要做的参数
        /*if (action == Constants.KEY_DOWNLOAD_ACTION_ADD) {
            entry.status = DownloadStatus.downloading;
            //更新状态，这里要通知我们的上层也就是观察者，告诉用户别再点了已经开始下载了
            //调用被观察者中的方法
            DataChanger.getmInstance().postStatus(entry);
            entry.totalLength = 1024 * 5;
            for (int i = 0; i < entry.totalLength; ) {
                i += 1024;
                entry.currentLength += 1024;
                DataChanger.getmInstance().postStatus(entry);
            }
            entry.status = DownloadStatus.completed;
            DataChanger.getmInstance().postStatus(entry);
        }*/
        switch (action) {
            case Constants.KEY_DOWNLOAD_ACTION_ADD:
                startDownload(entry);
                break;
            case Constants.KEY_DOWNLOAD_ACTION_PAUSE:
                pauseDownload(entry);
                break;
            case Constants.KEY_DOWNLOAD_ACTION_RESUME:
                resumeDownload(entry);
                break;
            case Constants.KEY_DOWNLOAD_ACTION_CANCEL:
                cancelDownload(entry);
                break;

        }
    }

    private void startDownload(DownloadEntry entry) {
        DownloadTask task = new DownloadTask(entry);
        //task.start();
        mDownloadingTasks.put(entry.id, task);
        //这样开始下载操作就跑到子线程中进行了
        mExecutors.execute(task);

    }

    private void pauseDownload(DownloadEntry entry) {
        DownloadTask task = mDownloadingTasks.remove(entry.id);
        if (task != null) {
            task.pause();
        }
    }

    private void resumeDownload(DownloadEntry entry) {
        startDownload(entry);
    }

    private void cancelDownload(DownloadEntry entry) {
        DownloadTask task = mDownloadingTasks.remove(entry.id);
        if (task != null) {
            task.cancle();
        }
    }
}
